package com.lx;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author c先生
 * @date 2022年04月14日 上午 8:38:09
 * @describe
 */

public class Util_lx {
    public Util_lx() {
        this.init();
    }

    HashMap<String, List> playerMap = new HashMap<>();

    //删除球员信息
    public boolean deletePlayer(String name){
        Set<String> keySet = playerMap.keySet();
        for (String playerKey : keySet) {
            List<Player_lx> playerList = playerMap.get(playerKey);
            for (Player_lx player : playerList) {
                if(player.getPlayerName().equals(name)){
                    playerList.remove(player);
                    return true;
                }
            }
        }
        return false;
    }

    //修改球员信息
    public Player_lx updatePlayer(String name){
        Set<String> keySet = playerMap.keySet();
        Player_lx player1 = null;
        for (String playerKey : keySet) {
            List<Player_lx> playerList = playerMap.get(playerKey);
            for (Player_lx player : playerList) {
                if(player.getPlayerName().equals(name)){
                    player1 = player;
                }
            }
        }
        return player1;
    }

    //新增一名球员信息
    public boolean addPlayer(String qiudui,Player_lx player){
            Set<String> keySet = playerMap.keySet();
            boolean flag = false;
            for (String playerKey : keySet) {
                if(playerKey.equals(qiudui)){
                    List<Player_lx> playerList = playerMap.get(playerKey);
                    playerList.add(player);
                    playerMap.put(playerKey,playerList);
                    flag = true;
                    break;
                }
            }
            if(flag == false) {
                List<Player_lx> playerListList2 = new ArrayList<>();
                playerListList2.add(player);
                playerMap.put(qiudui, playerListList2);
                flag = true;
            }
            return flag;
    }

    //根据位置展示球员信息
    public void showPlayerInfoByJob(String s){
        Set<String> keySet = playerMap.keySet();
        for (String playerKey : keySet) {
            List<Player_lx> playerList = playerMap.get(playerKey);
            for (Player_lx player : playerList) {
                if(player.getPlayerJob().equals(s)){
                    System.out.println(player.toString());
                }
            }
        }
    }


    //展示所有球员信息
    public void showAllPlayer(){
        Set<String> keySet = playerMap.keySet();

        for (String playerKey : keySet) {//删除没有球员的球队
            if(playerMap.get(playerKey).isEmpty() || playerMap.get(playerKey) == null
                    || playerMap.get(playerKey).equals("")){
                playerMap.remove(playerKey);
            }
        }
        for (String playerKey : keySet) {
            System.out.println(playerKey+"球员如下：");
            List<Player_lx> playerList = playerMap.get(playerKey);
            for (Player_lx player : playerList) {
                System.out.println(player.toString());
            }
        }
    }

    //初始化
    public void init(){
        String qiudui1 = "湖人队";
        List<Player_lx> playeres1 = new ArrayList<>();
        Player_lx p1 = new Player_lx(1001,"张恒","守门员","150cm",69);
        Player_lx p2 = new Player_lx(1002,"张恒2","后卫","151cm",39);
        Player_lx p3 = new Player_lx(1003,"张恒3","前锋","152cm",19);
        playeres1.add(p1);
        playeres1.add(p2);
        playeres1.add(p3);
        playerMap.put(qiudui1,playeres1);

        String qiudui2 = "342队";
        List<Player_lx> playeres2 = new ArrayList<>();
        Player_lx p4 = new Player_lx(1004,"周煜民","前锋","171cm",10);
        Player_lx p5 = new Player_lx(1005,"杨威","中卫","200cm",9);
        playeres2.add(p4);
        playeres2.add(p5);
        playerMap.put(qiudui2,playeres2);
    }
}
